﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class Pages_Admin_AdaugareFirma : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      NumeBrand_succes_panel.Visible = false;
      NumeBrand_error_panel.Visible = false;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
      NumeBrand_tb.Text = NumeBrand_tb.Text.Trim();
      if (NumeBrand_tb.Text.Length < 5 || NumeBrand_tb.Text.Length > 50)
      {
        NumeBrand_error_tb.Text = "Numele Brandului trebuie sa fie intre 5 si 50 de caractere";
        NumeBrand_error_panel.Visible = true;
        return;
      }

      //adaug in baza de date
      try
      {
        SqlConnection connection = new SqlConnection(GetConnectionString());      //deschid conexiune catre baza de date
        SqlCommand command = new SqlCommand("Brand_Insert", connection);          //invoc procedura stocata
        command.CommandType = CommandType.StoredProcedure;                        //setez ca o sa chem o procedura stocata

        command.Parameters.AddWithValue("@brand_name", NumeBrand_tb.Text);

        connection.Open();
        int succes = command.ExecuteNonQuery();

        if (succes == 1)
        {
          NumeBrand_succes_tb.Text = "Inserare reusita";
          NumeBrand_succes_panel.Visible = true;
        }
        else
        {
          NumeBrand_error_tb.Text = "Brand existent deja";
          NumeBrand_error_panel.Visible = true;
        }
      }
      catch (Exception E)
      {
        Console.WriteLine(E.StackTrace);
      }
      
    }
  #region "DBA"

    string GetConnectionString(string connection_name = "StoreDBConnectionString")
    {
      string connection = string.Empty;

      connection = ConfigurationManager.ConnectionStrings[connection_name].ConnectionString;

      return connection;
    }
  #endregion
}